/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bff.gaia.api.common.operators.base;

import com.bff.gaia.annotation.Internal;
import com.bff.gaia.api.common.functions.FlatJoinFunction;
import com.bff.gaia.api.common.operators.BinaryOperatorInformation;
import com.bff.gaia.api.common.operators.util.UserCodeObjectWrapper;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

@Internal
public class InnerJoinTreeOperatorBase<IN1, IN2, OUT, FT extends FlatJoinFunction<IN1, IN2, OUT>> extends InnerJoinOperatorBase<IN1, IN2, OUT, FT> {

	private ColumnInfo columnInfo;

	public InnerJoinTreeOperatorBase(FT udf, BinaryOperatorInformation<IN1, IN2, OUT> operatorInfo, int[] keyPositions1,
									 int[] keyPositions2, String name) {
		super(new UserCodeObjectWrapper<FT>(udf), operatorInfo, keyPositions1, keyPositions2, name);
	}

	public static class ColumnInfo {
		private int[] keySetPos;
		private int[] resultPos;
		private Set<Integer> finishedNodeSet;

		public ColumnInfo(int[] keySetPos, int[] resultPos, int id) {
			this.keySetPos = keySetPos;
			this.resultPos = resultPos;
			finishedNodeSet = new HashSet<>();
			finishedNodeSet.add(id);
		}

		public ColumnInfo(ArrayList<Integer> keySetPos, ArrayList<Integer> resultPos, Set<Integer> finishedNodeSet) {
			this.keySetPos = keySetPos.stream().mapToInt(Integer::intValue).toArray();
			this.resultPos = resultPos.stream().mapToInt(Integer::intValue).toArray();
			this.finishedNodeSet = finishedNodeSet;
		}

		public int[] getKeySetPos() {
			return keySetPos;
		}

		public int[] getResultPos() {
			return resultPos;
		}

		public Set<Integer> getFinishedNodeSet() {
			return finishedNodeSet;
		}
	}

	public void setColumnInfo(ColumnInfo columnInfo) {
		this.columnInfo = columnInfo;
	}

	public ColumnInfo getColumnInfo() {
		return columnInfo;
	}
}